﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Present.Commands.Async.Actions;
using Present.Commands.Async.Actions.Decorators;
using Present.Commands.Fluent;

namespace Present.Commands.Sample
{
    /// <summary>
    /// Example Decorator, sleeps for the specified count
    /// </summary>
    /// <typeparam name="TParameter"></typeparam>
    /// <typeparam name="TResult"></typeparam>
    public class SleepAsyncActionDecorator<TParameter, TResult> : AsyncActionDecorator<TParameter, TResult>
    {
        public SleepAsyncActionDecorator(IAsyncAction<TParameter, TResult> innerTarget) : base(innerTarget)
        {
        }

        public TimeSpan SleepTime { get; set; }

        public override TResult DoWork(ActionExecution<TParameter> args)
        {
            Thread.Sleep(SleepTime);
            return base.DoWork(args);
        }
    }

    public static class SleepAsyncActionDecoratorBuilder
    {

        public static ActionBuilder<TParameter, TResult> Sleep<TParameter, TResult>(this ActionBuilder<TParameter, TResult> builder, TimeSpan sleep)
        {
            return new SimpleActionBuilder<TParameter, TResult>(() => new SleepAsyncActionDecorator<TParameter, TResult>(builder.ToAction()) { SleepTime = sleep });
        }
    }


}
